const query = require('@/modules/database')

module.exports.count = () => {
  let _sql = 'SELECT count(id) as count FROM article';
  return query(_sql)
}

module.exports.list = (req) => {
  const params = req.query
  let _sql = 'SELECT * FROM article';
  params.title && (_sql += ` WHERE title LIKE '%${params.title || ''}%'`)
  params.tag && (_sql += ` WHERE tags LIKE '%${params.tag || ''}%'`)
  params.category && (_sql += ` WHERE category LIKE '%${params.category || ''}%'`)
  params.page && params.size && (_sql += ` limit ${(params.page - 1) * params.size},${params.size}`);
  return query(_sql)
}

module.exports.detail = (req) => {
  let _sql = `SELECT * FROM article WHERE id=${req.query.id}`;
  return query(_sql)
}

module.exports.insert = (req) => {
  let _sql = `INSERT INTO article SET title=?,summary=?,content=?,status=?`
  return query(_sql, _.values(req.body))
}

module.exports.update = (req) => {
  let _sql = `UPDATE article SET title=?,summary=?,content=?,status=? WHERE id=${req.body.id}`
  delete req.body.id
  return query(_sql, _.values(req.body))
}

module.exports.remove = (req) => {
  let _sql = `DELETE FROM article WHERE id = ${req.body.id}`
  return query(_sql)
}
